<?php

/*
 * 
 * 
 *  This software has been made FREE by author for the interest of other developers. 
 *       I hope this will help you in coding faster and robust php code
 *             You can modify and redistribute it as you like.
 *               Your comments, suggestions and corrections 
 *                           will be highly 
 *                            APPRECIATED. 
 *                                       Author
 * @author :   Roshan Budhathoki (developer.roshan@gmail.com);
 * @URL    :   <http://www.roshanbudhathoki.com>
 * 
 */

class Application extends base {
	protected $_view;
	function __construct() {
	
	}
	public function run() {
		// DO SEARCH MODEL
		$top=Model_Mapper_Categories::newInstance()->getTopCategories(true);
		view::newInstance()->_exportVariableToView('main_categories', $top);
		//view::newInstance()->_get('main_categories');
		
		$search_category = Params::getParam ( 'c' );
		$search_string = Params::getParam ( 's' );
		$search_location = Params::getParam ( 'in' );
		$location = '';
		$category = '';
		$args = array ();
		$args ['query'] = '';
		$args ['having'] = '';
		$args ['where'] = '';
		$args ['expr'] = '';
		$args ['order'] = '';
		$args ['limit'] = '';
		
		/*
		 * *********************
		 * Defaults
		 * *********************
		 */
		
		$title_only = false;
		$with_picture_only = false;
		$min_price = ( float ) Params::getParam ( 'min_price' );
		$max_price = ( float ) Params::getParam ( 'max_price' );
		$sort = Params::getParam ( 'sort' );
		if ($sort == '') {
			$sort = 'date-desc';
		}
		
		$sort = str_replace ( '-', ' ', $sort );
		$sort = str_replace ( 'date', 'publish_date', $sort );
		$args ['order'] [] = $sort;
		
		if (is_numeric ( $min_price ) && is_numeric ( $max_price ) && $min_price >= 0 && $max_price > 0) {
			$args ['where'] ['items.price >=?'] = $min_price;
			$args ['where'] ['items.price <=?'] = $max_price;
		}
		
		/*
		 * ************************
		 * ADD Location if set
		 * ************************
		 */
		
		if (! $search_location == '') {
			// check postcode or location
			//$reg = '^([A-PR-UWYZ0-9][A-HK-Y0-9][AEHMNPRTVXY0-9]?[ABEHMNPRVWXY0-9]? {1,2}[0-9][ABD-HJLN-UW-Z]{2}|GIR 0AA)$';
			$reg = '^[A-Za-z]((([0-9]{1,2})|([A-Za-z][0-9]{1,2})|([A-Za-z][0-9][A-Za-z])))$';
			//$reg='^(([Gg][Ii][Rr] 0[Aa]{2})|((([A-Za-z][0-9]{1,2})|(([A-Za-z][A-Ha-hJ-Yj-y][0-9]{1,2})|(([A-Za-z][0-9][A-Za-z])|([A-Za-z][A-Ha-hJ-Yj-y][0-9]?[A-Za-z])))) [0-9][A-Za-z]{2}))$';
			if (preg_match ( '#' . $reg . '#', $search_location, $m )) {
				$args ['where'] ['postcode LIKE ?'] = '%' . $search_location . '%';
			} else {
				$location = Model_Mapper_Locations::newInstance ()->find ( 'name Like \'%' . $search_location . '%\'' );
				if ($location) {
					$location = $location [0];
					Params::setParam ( 'search_location', $location );
					$args ['where'] ['location_id'] = $location->getLocation_id ();
				}
			}
		
		}
		
		/*
		 * ADD CATEGORY IF SPECIFIED
		 */
		if ($search_category != '' && $search_category != 'all') {
			$category = Model_Mapper_Categories::newInstance ()->getBySlug ( $search_category );
			Params::setParam ( 'search_category', $category );
			$cats = $category->getSubCategories ( true, true );
			$ids = array ();
			$ids [] = $category->getCategory_id ();
			if ($cats) {
				foreach ( $cats as $cat ) {
					$ids [] = $cat ['category_id'];
				}
			}
			$args ['where'] ['category_id IN (?)'] = $ids;
		}
		$search_string = str_replace ( '+', ' ', $search_string );
		$query = array ();
		if ($title_only) {
			$args ['expr'] [] = 'MATCH (name) AGAINST (' . $search_string . ') as score ';
		} else {
			$args ['expr'] [] = 'MATCH (name,description) AGAINST (\'' . $search_string . '\') as score ';
		}
		if ($with_picture_only) {
			$args ['expr'] [] = '(SELECT count(*) FROM item_resource WHERE item_id=items.item_id) as image_count ';
			$args ['having'] [] = 'image_count>0';
		}
		
		$total_items = Model_Mapper_Items::newInstance ()->find ( $query, $args );
		$args ['limit'] ['count'] = 50;
		$args ['limit'] ['page'] = 1;
		$items = Model_mapper_Items::newInstance ()->find ( $query, $args );
		view::newInstance ()->_exportVariableToView ( 'items', $items );
		$this->dispatch ( 'search.phtml' );
	}

}